//
//  DiscoverViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/6.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "DiscoverViewController.h"
#import "SearchBar.h"

@interface DiscoverViewController ()<UITextFieldDelegate>

@property (nonatomic,weak) UITextField *searchTexFiled;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];

    //自定义textFiled
    
    SearchBar *searchTexFiled = [[SearchBar alloc]initWithFrame:CGRectMake(0, 0, 280, 30)];
    
    self.searchTexFiled = searchTexFiled;
//    文本框默认不显示，要设置他的边框
//    searchTexFiled.borderStyle = UITextBorderStyleRoundedRect;
//    
//    searchTexFiled.placeholder = @"请输入你要搜索的内容";
//    
//    searchTexFiled.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_searchlist_search_icon"]];
//    
//    searchTexFiled.leftViewMode = UITextFieldViewModeAlways;
//    
//    //清除按钮
//    searchTexFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    self.navigationItem.titleView = searchTexFiled;
    
    //监听文本框，设置代理
    searchTexFiled.delegate = self;
    
    
}


#pragma mark - UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    //添加右边取消按钮
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickCancel)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem setNavItemWithTitle:@"取消" target:self action:@selector(clickCancel)];
    //改变左边图标
    self.searchTexFiled.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"settings_statistic_triangle"]];
    
    
    
}


-(void)clickCancel{
    

    //关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    //图标转回
    self.searchTexFiled.leftView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_searchlist_search_icon"]];

}



-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    //关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    //图标转回
    self.searchTexFiled.leftView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_searchlist_search_icon"]];

    
}








@end
